const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const util = require('../../utils/util.js');

Page({
  data: {
    current:0,
    banner:[],
    meau:["全部","一日活动","两日活动","端午活动","十一活动","境外活动"],
    loading: false,
    bottomLift: 0,
    currentid:'',
    page:1,
    pageSize:10,
    isEnd:false,
    contentlistdata:[],
    isloading:true,
    user:"",
    isMask:false
  },

  onLoad() {
    let {bottomLift} = app.globalData
    if(bottomLift>=0){
      app.getDeviceSize().then(res => {
        const {bottomLift} = res
        this.setData({
          bottomLift
        })
      })
    }else{
      this.setData({
        bottomLift
      })
    }
    this.bannerList()
    this.taglist()
    this.activitylist()
  },
  onShow(){
    this.isblacklist()
  },
  bannerList(){
    Controller.bannerList({source:1}).then(res=>{
      if(res.data.baseData){
        this.setData({
          banner:res.data.baseData
        })
      }
    })
  },
  taglist(){
    Controller.taglist().then(res=>{
      if(res.data.baseData){
        var data=[{title:'全部',id:''}]
        res.data.baseData=data.concat(res.data.baseData)
        this.setData({
          meau:res.data.baseData
        })
      }
    })
  },
  
  tabs(e) {
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id
    this.setData({
      current: index,
      currentid:id || '',
      page:1,
      pageSize:10,
      isEnd:false,
      contentlistdata:[],
      isloading:true,
    })
    this.activitylist()
  },
  activitylist(){
    Controller.activitylist({categoryId:this.data.currentid,page:this.data.page,pageSize:this.data.pageSize}).then(res=>{
      if(res.data.baseData && res.data.baseData.list.length>0){
        var lists=this.data.contentlistdata.concat(res.data.baseData.list);
        var item=[];
        var newslists=[];
        for (var i = 0; i < lists.length; i++){
          if (lists[i].status ==-2){
            item.push(lists[i]);
          }else{
            newslists.push(lists[i]);
          }
        }
        newslists=newslists.concat(item)
        this.setData({
          contentlistdata:newslists,
        })
      }
      if(res.data.baseData.lastPage){
        this.setData({
          isEnd:res.data.baseData.lastPage
        })
      }
      this.setData({
        isloading:false
      })
    })
  },
  onReachBottom(){
    if(this.data.isEnd){
      return
    }else{
      this.setData({
        page:this.data.page+1
      })
      this.activitylist()
    }
    
  },
  golink(e){
    var id=e.currentTarget.dataset.id;
    var status=e.currentTarget.dataset.status;
    if(status==-2){
      return
    }
    
    wx.navigateTo({
      url:e.currentTarget.dataset.url
    });
  },
  isblacklist(){

    if(!wx.getStorageSync('logintoken')){
      return;
    }

    Controller.info({}).then((res)=>{
      if(res.data.baseData.role.userType ==5){
          this.setData({
            isMask:true
          })
          wx.showToast({
            title: '无法打开',
            icon: 'none',
            duration: 2000*999999,
            mask:true
          })
      }

    }).catch((res)=>{
        if(res.code == 102 || res.code == -9){
          // console.log(res)
            this.setData({
              isMask:true
            })
            wx.showToast({
              title: '无法打开',
              icon: 'none',
              duration: 2000*999999,
              mask:true
            })
        }
    });

  },
  onPageScroll: function (e) { // 获取滚动条当前位置
    
  },
  onShareAppMessage: function (e) {
    
  },
  onShareTimeline: function(res){
    
  },
})